package com.xiaomaoguai.word.filter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/*
 * @Description :初始化敏感词库，将敏感词加入到HashMap中，构建DFA算法模型
 * @FileName: SensitiveWordInit.java
 * @Data : 2014年12月16日 上午11:09:39
 * @Version:1.0.0
 */
public class SensitiveWordInit {

	private String ENCODING = "GBK"; // 字符编码
	public HashMap<String, String> sensitiveWordMap;

	public SensitiveWordInit() {
		super();
	}

	public Map<String, String> initKeyWord() {
		try {
			// 读取敏感词库
			Set<String> keyWordSet = readSensitiveWordFile();
			// 将敏感词库加入到HashMap中
			addSensitiveWordToHashMap(keyWordSet);
			// spring获取application，然后application.setAttribute("sensitiveWordMap",sensitiveWordMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sensitiveWordMap;
	}

	/**
	 * 读取敏感词库，将敏感词放入HashSet中，构建一个DFA算法模型：<br>
	 * 中 = { isEnd = 0 国 = {<br>
	 * isEnd = 1 人 = {isEnd = 0 民 = {isEnd = 1} } 男 = { isEnd = 0 人 = { isEnd =
	 * 1 } } } } 五 = { isEnd = 0 星 = { isEnd = 0 红 = { isEnd = 0 旗 = { isEnd = 1
	 * } } } }
	 * 
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void addSensitiveWordToHashMap(Set<String> keyWordSet) {
		sensitiveWordMap = new HashMap(keyWordSet.size()); // 初始化敏感词容器，减少扩容操作
		String key = null;
		Map nowMap = null;
		Map newWorMap = null;
		// 迭代keyWordSet
		Iterator<String> iterator = keyWordSet.iterator();
		while (iterator.hasNext()) {
			key = iterator.next(); // 关键字
			nowMap = sensitiveWordMap;
			for (int i = 0; i < key.length(); i++) {
				char keyChar = key.charAt(i); // 转换成char型
				Object wordMap = nowMap.get(keyChar); // 获取

				if (wordMap != null) { // 如果存在该key，直接赋值
					nowMap = (Map) wordMap;
				} else { // 不存在则，则构建一个map，同时将isEnd设置为0，因为他不是最后一个
					newWorMap = new HashMap<String, String>();
					newWorMap.put("isEnd", "0"); // 不是最后一个
					nowMap.put(keyChar, newWorMap);
					nowMap = newWorMap;
				}

				if (i == key.length() - 1) {
					nowMap.put("isEnd", "1"); // 最后一个
				}
			}
		}
	}

	/**
	 * 读取敏感词库中的内容，将内容添加到set集合中
	 */
	private Set<String> readSensitiveWordFile() {
		Set<String> set = new HashSet<String>();
		//String wordPath = System.getProperty("word.path");// 敏感词路径
		File file = new File("D:/filterWord");
		//File file = new File(wordPath);
		//Assert.assertTrue("敏感词路径不是一个目录", file.isDirectory());
		File[] listFiles = file.listFiles();
		int totalFile = listFiles.length;
		int i = 0;
		for (File txtFile : listFiles) {
			InputStreamReader read = null;
			BufferedReader bufferedReader = null;
			try {
				read = new InputStreamReader(new FileInputStream(txtFile), ENCODING);
				if (txtFile.isFile() && txtFile.exists()) { // 文件流是否存在
					System.out.println(txtFile.getName());
					bufferedReader = new BufferedReader(read);
					String txt = null;
					while ((txt = bufferedReader.readLine()) != null) { // 读取文件，将文件内容放入到set中
						set.add(txt);
						i++;
					}
				}
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (totalFile == i) {
					try {
						bufferedReader.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
					try {
						read.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
		return set;
	}
}